package com.designMode.interpreter.nonterminal;

import com.designMode.interpreter.AbstractExpression;
import com.designMode.interpreter.Context;
import com.designMode.interpreter.NonterminalExpression;

/**
 * 乘运算表达式
 */
public class Multiply extends NonterminalExpression {
    private final AbstractExpression left, right;

    public Multiply(AbstractExpression left, AbstractExpression right) {
        this.left = left;
        this.right = right;
    }

    @Override
    public Object interpret(Context ctx) {
        Object leftObj = left.interpret(ctx);
        Object rightObj = right.interpret(ctx);
        //参数检查
        checkInteger(leftObj,left.toString());
        checkInteger(rightObj,right.toString());
        return (Integer) leftObj * (Integer) rightObj;
    }

    @Override
    public boolean equals(AbstractExpression exp) {
        if (exp != null && exp instanceof Multiply) {
            return left.equals(((Multiply) exp).left) &&
                    right.equals(((Multiply) exp).right);
        }
        return false;
    }

    @Override
    public String toString() {
        return "(" + left.toString() + " * " + right.toString() + ")";
    }
}
